package com.example.workrecord.util.dingtalkopenapi.department;


import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.example.workrecord.exption.BizException;

/**
 * @ClassName: DepartmentHelper
 * @Description 部门相关API
 * https://open-doc.dingtalk.com/docs/doc.htm?treeId=371&articleId=106817&docType=1
 * @Author Mr.Bu
 * @Date 2019/7/24 13:29
 */
public class DepartmentHelper {
    /**
     * 创建部门
     */
    public static String createDepartment(String accessToken, String name,
                                          String parentId, String order, boolean createDeptGroup) throws Exception {


        DingTalkClient client=new DefaultDingTalkClient("https://oapi.dingtalk.com/department/create");
        OapiDepartmentCreateRequest request=new OapiDepartmentCreateRequest();
        request.setParentid(parentId);
        request.setCreateDeptGroup(createDeptGroup);
        request.setName(name);
        OapiDepartmentCreateResponse response=client.execute(request, accessToken);
        if (!"0".equals(response.getErrcode().toString())) {
            throw new BizException("创建部门异常：" + response.getErrmsg());
        }
        return response.getId().toString();
    }
//    /**
//     * 获取部门列表
//     */
//    public static List<OapiDepartmentListResponse.Department> listDepartments(String accessToken, String parentDeptId)
//            throws ServiceNotExistException, SdkInitException, ServiceException {
//        CorpDepartmentService corpDepartmentService = ServiceFactory.getInstance().getOpenService(CorpDepartmentService.class);
//        List<Department> deptList = corpDepartmentService.getDeptList(accessToken, parentDeptId);
//        return deptList;
//    }


    /**
     * 删除部门
     */
    public static void deleteDepartment(String accessToken, Long id) throws Exception {
        DingTalkClient client=new DefaultDingTalkClient("https://oapi.dingtalk.com/department/delete");
        OapiDepartmentDeleteRequest request=new OapiDepartmentDeleteRequest();
        request.setId(id.toString());
        request.setHttpMethod("GET");
        OapiDepartmentDeleteResponse response=client.execute(request, accessToken);
        if (!response.isSuccess()) {
            System.out.println("***********部门删除失败，失败部门的信息为" + id + "原因" + response.getErrmsg());
            throw new Exception("部门删除失败，失败部门的信息为" + id + "原因" + response.getErrmsg());
        }


    }

    /**
     * 更新部门
     */
    public static void updateDepartment(String accessToken, long id, String name,
                                        String parentId) throws Exception {
        DingTalkClient client=new DefaultDingTalkClient("https://oapi.dingtalk.com/department/update");
        OapiDepartmentUpdateRequest request=new OapiDepartmentUpdateRequest();
        request.setId(id);
        request.setParentid(parentId);
//        request.setOrder("200");
        request.setName(name);
        OapiDepartmentUpdateResponse response=client.execute(request, accessToken);
        if (!"0".equals(response.getErrcode().toString())) {
            throw new BizException("创建部门异常：" + response.getErrmsg());
        }

    }

    public static OapiUserGetDeptMemberResponse getDeptMember(String accessToken, String dingId) throws Exception {
        DingTalkClient client=new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember");
        OapiUserGetDeptMemberRequest req=new OapiUserGetDeptMemberRequest();
        req.setDeptId(dingId);
        req.setHttpMethod("GET");
        OapiUserGetDeptMemberResponse execute=client.execute(req, accessToken);
        if (!execute.isSuccess()) {
            throw new BizException("部门人员获取异常" + execute.getErrmsg());
        }
        return execute;
    }



    public static OapiDepartmentGetResponse getOapiDepartmentGetResponse(String deptId, String accseToken) throws Exception {
        DingTalkClient client=new DefaultDingTalkClient("https://oapi.dingtalk.com/department/get");
        OapiDepartmentGetRequest request=new OapiDepartmentGetRequest();
        request.setId(deptId);
        request.setHttpMethod("GET");
        OapiDepartmentGetResponse response=client.execute(request, accseToken);
        if (!response.isSuccess()) {
            throw new BizException("部门信息获取异常，请检查！" + response.getErrmsg());
        }
        return response;
    }
}
